package zhizhemodi.tech.short_term_bwork.tool;

import lombok.Getter;

@Getter
public class CommonResult<T> {
    private Integer code;
    private String message;
    private T data;

    public CommonResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 封装好的信息
     * @param <T>
     * @param t
     * @return
     */
    public static <T> CommonResult<?> success(T t) {
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), t);
    }

    /**
     * 重载自定义message
     * @param <T>
     * @param t
     * @param message
     * @return
     */
    public static <T> CommonResult<?> success(T t, String message) {
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), message, t);
    }

    /**
     * 重载自定义code message
     * @param <T>
     * @param t
     * @param code
     * @param message
     * @return
     */
    public static <T> CommonResult<?> success(T t, Integer code, String message) {
        return new CommonResult<T>(code, message, t);
    }

    public static <T> CommonResult<?> failed(String message) {
        return new CommonResult<T>(ResultCode.FAIL.getCode(), message, null);
    }

    public static <T> CommonResult<?> failed(ResultCode resultCode) {
        return new CommonResult<T>(resultCode.getCode(), resultCode.getMessage(), null);
    }

    public static <T> CommonResult<?> failed(ResultCode resultCode, T data) {
        return new CommonResult<T>(resultCode.getCode(), resultCode.getMessage(), data);
    }

    public static <T> CommonResult<?> failed() {
        return failed(ResultCode.FAIL);
    }

    public static <T> CommonResult<?> validateFailed() {
        return failed(ResultCode.VALIDATE_FAILED);
    }

    public static <T> CommonResult<?> validateFailed(String message) {
        return new CommonResult<T>(ResultCode.VALIDATE_FAILED.getCode(), message, null);
    }

    public static <T> CommonResult<?> unauthorized(T data) {
        return failed(ResultCode.UNAUTHORIZED, data);
    }

    public static <T> CommonResult<?> forbidden(T data) {
        return failed(ResultCode.FORBIDDEN, data);
    }
}
